package com.eric.media.camera;

import android.Manifest;
import android.hardware.Camera;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.eric.media.R;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

@EActivity(R.layout.activity_camera_test)
public class CameraTestActivity extends AppCompatActivity {
    @ViewById(R.id.sv_camera)
    SurfaceView surfaceView;
    private CameraUtil cameraUtil;

    @Override
    protected void onResume() {
        super.onResume();
        cameraUtil = CameraUtil.getInstance();
        checkSelfPermission();
    }

    @AfterInject
    void inject(){
        //设置全屏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    }
    @AfterViews
    void initData(){
    }

    @Click({R.id.imgBtn_back,R.id.img_take,R.id.video_view})
    void onClick(View view){
        switch (view.getId()){
            case R.id.imgBtn_back:
                finish();
                break;
            case R.id.img_take:
                //开始拍照
                cameraUtil.takePicture();
               break;
            case R.id.video_view:
                cameraUtil.requestFocus();
               break;
        }
    }
    /**
     * 动态检查权限
     */
    private void checkSelfPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            //动态判断手机储存空间的权限
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == -1 ||
                    ActivityCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE ) == -1 ||
                    ActivityCompat.checkSelfPermission(this,Manifest.permission.CAMERA) == -1){
                ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CAMERA},0x123);
            }else {
                surfaceView.post(new Runnable() {
                    @Override
                    public void run() {
                        cameraUtil.initCamera(CameraTestActivity.this,surfaceView,false);
                    }
                });
            }
        }else {
            surfaceView.post(new Runnable() {
                @Override
                public void run() {
                    cameraUtil.initCamera(CameraTestActivity.this,surfaceView,false);
                }
            });
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 0x123){
            if (grantResults[0] == 0 && grantResults[1] == 0){
                surfaceView.post(new Runnable() {
                    @Override
                    public void run() {
                        cameraUtil.initCamera(CameraTestActivity.this,surfaceView,false);
                    }
                });
            }
        }
    }
}
